package com.gomtel.smartdevice.ui.activity;

import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.api.MessageManager;
import com.gomtel.smartdevice.base.WatchAppBaseActivity;
import com.gomtel.smartdevice.bean.AbourAllTypeBean;
import com.gomtel.smartdevice.global.DataClass;
import com.gomtel.smartdevice.global.WatchApplication;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.moudle.rxtools.EventBusUtil;
import com.gomtel.smartdevice.ui.dialog.ShowDialog;
import com.gomtel.smartdevice.utils.LogUtil;
import com.littlejie.circleprogress.DialProgress;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/17 0017.
 * 血压校准
 */

public class BPCActivity extends WatchAppBaseActivity implements View.OnClickListener {

    @BindView(R.id.img_btn_black)
    ImageButton img_btn_black;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.progress_bar)
    DialProgress progress_bar;
    @BindView(R.id.byoyomi)
    TextView byoyomi;
    @BindView(R.id.pvh_mmHg)
    TextView pvh_mmHg;
    @BindView(R.id.ldo_mmHg)
    TextView ldo_mmHg;

    private ShowDialog showDialog;
    private int max = 25;
    private int ss = 28;
    private int cc = 0;
    private boolean status = false;
    private String inputPvh = "140";
    private String inputLdo = "72";

    private Handler handler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (byoyomi != null)
                        byoyomi.setText(String.valueOf(ss - max));
                    break;
                case 1:
                    byoyomi.setText(getString(R.string.current_test));
                    progress_bar.setValue(cc++);
                    break;
                case 2:
                    initView();
                    showDialog.showStatusDialog(BPCActivity.this, EventCode.TEST_IS_WRONG);
                    break;
                case 3:
                    initView();
                    break;
            }
            return true;
        }
    });

    @Override
    protected void registerEvent(CommonEvent commonevent) {
        switch (commonevent.getCode()) {
            case EventCode.OPEN_BLOOD_PRESSURE_CALIBRATION:
                showDialog.showBPCDialog(this, 0, 0);
                break;
            case EventCode.ACTION_BPC_OFF:
                finish();
                break;
            case EventCode.ACTION_BPC:
                AbourAllTypeBean object = (AbourAllTypeBean) commonevent.getObject();
                inputPvh = object.getPvh();
                inputLdo = object.getLdo();
                showDialog.showStatusDialog(this, EventCode.INSTRUCTIONS);
                break;
            case EventCode.INSTRUCTIONS:
                RunBpc();
                break;
            case EventCode.UNDERWAY:
                finish();
                break;
            case EventCode.TEST_IS_WRONG:
                RunBpc();
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_bpc;
    }

    @Override
    protected void initClass() {
        showDialog = ShowDialog.newInstance();
        showDialog.showStatusDialog(this, EventCode.OPEN_BLOOD_PRESSURE_CALIBRATION);
    }

    @Override
    protected void initData() {
        if (!DataClass.onlineStatus) {
            toastUtil.showToast("设备未连接 ... ");
            finish();
        }
    }

    @Override
    protected void initView() {
        ss = 28;
        status = false;
        title_name.setText(getString(R.string.Blood_pressure_calibration));
        progress_bar.reset();
        byoyomi.setText(String.valueOf(ss - max));
        progress_bar.setValue(max);
    }

    @Override
    protected void initListener() {
        img_btn_black.setOnClickListener(this);
        BpListener();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_btn_black:
                finish();
                break;
        }
    }

    private void RunBpc() {
        if (!DataClass.onlineStatus) {
            toastUtil.showToast("设备未连接 ... ");
            finish();
            return;
        }
        EventBusUtil.sendEvent(new CommonEvent(EventCode.BPCSTANDARD));
        ssOrRun();
    }

    private void ssOrRun() {
        status = true;
        WatchApplication.executorService.submit(() -> {
            try {
                ss--;
                Thread.sleep(999);
                if (ss > 0 && ss > max && status) {
                    handler.sendEmptyMessage(0);
                } else if (ss > 0 && ss < max && status) {
                    handler.sendEmptyMessage(1);
                } else if (ss < 0) {
                    handler.sendEmptyMessage(3);
                }
                if (status)
                    ssOrRun();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    private void BpListener() {
        MessageManager.getInstance().setBpListener(bpResponse -> {
            refreshLogView("systolic = " + bpResponse.getSystolic() + ", diastolic = " + bpResponse.getDiastolic() + ",hr = " + bpResponse.getHr() + ", pwtt = " + bpResponse.getPwtt());
            // 血压校准
            if (DataClass.isCalibration) {
                DataClass.isCalibration = false;
                int sys = bpResponse.getSystolic();
                int dias = bpResponse.getDiastolic();
                if (sys == 0 && dias == 0) {
                    refreshLogView("设备已取消校准");
                    handler.sendEmptyMessage(3);
                } else if (sys == 255 && dias == 255) {
                    refreshLogView(" 校准有误,请重试");
                    handler.sendEmptyMessage(2);
                } else {
                    pvh_mmHg.setText(String.valueOf(sys));
                    ldo_mmHg.setText(String.valueOf(dias));
                    // 传入两次医疗设备检测的血压值,两次的血压差最好不要大于10,
                    MessageManager.getInstance().sendBpDifference(Integer.valueOf(inputPvh), Integer.valueOf(inputLdo), Integer.valueOf(inputPvh), Integer.valueOf(inputLdo));
                }
            }
        });
    }

    public void refreshLogView(String msg) {
        LogUtil.e(TAG, "refreshLogView : " + msg);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (status) {
                showDialog.showStatusDialog(this, EventCode.UNDERWAY);
                return status;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
